package com.mce.nebula.controller;

import com.mce.nebula.dto.ApiResponse;
import com.mce.nebula.entity.Enterprise;
import com.mce.nebula.service.EnterpriseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/enterprises")
@Tag(name = "企业列表", description = "企业列表相关接口")
@RequiredArgsConstructor
public class EnterpriseListController {

    private final EnterpriseService enterpriseService;

    @GetMapping
    @Operation(
        summary = "获取企业列表",
        description = "获取企业列表，支持分页和搜索功能"
    )
    public ResponseEntity<ApiResponse<Map<String, Object>>> listEnterprises(
            @Parameter(description = "按企业名称, 企业别名搜索") 
            @RequestParam(required = false, defaultValue = "") String query,
            @Parameter(description = "页码") 
            @RequestParam(required = false, defaultValue = "1") String current,
            @Parameter(description = "每页数量") 
            @RequestParam(required = false, defaultValue = "10") String pageSize) {
        
        try {
            // 由于当前EnterpriseRepository没有实现搜索功能，暂时返回所有企业
            // TODO: 实现企业名称和别名的模糊搜索功能
            List<Enterprise> enterprises = enterpriseService.getAll();
            long total = enterprises.size();
            return ResponseEntity.ok(ApiResponse.success(new ApiResponse.ResponseBean<>(null,enterprises), "获取企业列表成功"));
        } catch (Exception e) {
            log.error("Failed to get enterprise list", e);
            return ResponseEntity.badRequest()
                    .body(com.mce.nebula.dto.ApiResponse.error(400, "获取企业列表失败: " + e.getMessage()));
        }
    }
}